<?php
namespace  app\model;
use app\utils\Md5Service;
use \think\facade\Db;
use think\facade\Log;

class LineModel
{


    public  static  function  lists($param)
    {
        $source = PartnerUser::where('id',request()->param('uid'))->value('source');
        $where[] = [
            'pl.uid', 'like',$source."%" ,
        ];
        $where[]=[
            'pl.partner_id','=',$param['partner_id'],
        ];
        if (isset($param['company_name']) && !empty($param['company_name'])){
            $where[]=['company.name','=',$param['company_name']];
        }
        if (isset($param['line_no']) && !empty($param['line_no'])){
            $where[]=['pl.line_no','=',$param['line_no']];
        }
        if (isset($param['line_name']) && !empty($param['line_name'])){
            $where[]=['pl.line_name','like', '%' .$param['line_name'] . '%'];
        }
        if (isset($param['driver_name']) && !empty($param['driver_name'])){
            $where[]=['driver.name','like', '%' .$param['driver_name'] . '%'];
        }
        if (isset($param['car_no']) && !empty($param['car_no'])){
            $where[]=['c.car_plate_number','like', '%'. $param['car_no'] . '%'];
        }                                
        if($param['is_export'] ==1){
            $param['limit'] = Db::table('xl_parnter_line')->alias('pl')->where([$where])->count();
        }
        $lineData=Db::table('xl_parnter_line')->alias('pl')
            ->join('xl_driver driver', 'driver.id = pl.defaut_driver_id', 'left')
            ->join('xl_cars c', 'c.id = pl.default_car_id', 'left')
            ->join('xl_company company', 'company.id = pl.company_id', 'left')
            ->field('company.name as company_name, driver.name as driver_name, c.car_plate_number as car_plate_number, c.owner_type, c.owner_name,pl.*')
            ->where("pl.status","=",1)
            ->where($where)
            ->limit(($param['page']-1) * $param['limit'], $param['limit'])
             ->select()->toArray();
       foreach ($lineData as $k=>$v){
           $list=[];
           $warehouse_ids=explode(",",$v['warehouse_ids']);
           foreach ($warehouse_ids as $k1=>$v1){
               $warehouseInfo=WarehouseModel::info($v1);

               $list[]=$warehouseInfo;
           }
           $network_ids=explode(",",$v['network_ids']);
           foreach ($network_ids as $k2=>$v2){
               $networkInfo=NetworksModel::info($v2);

               $list[]=$networkInfo;
           }
           $lineData[$k]['list']=$list;
       }

        $count= Db::table('xl_parnter_line')->alias('pl')
            ->join('xl_driver driver', 'driver.id = pl.defaut_driver_id', 'left')
            ->join('xl_cars c', 'c.id = pl.default_car_id', 'left')
            ->join('xl_company company', 'company.id = pl.company_id', 'left')
            ->where("pl.status","=",1)
            ->where($where)->count();
        return ['lineData'=>$lineData,'count'=>$count];
    }

    /**
     * 获取所有仓库类型
     * @return void
     */
    public static function GetAllWarehouseType()
    {
        return Db::table('xl_warehouse_type')->where("status","=",1)
            ->select();

    }
    /**
     * 获取所有业务类型
     * @return void
     */
    public static function GetAllBussineType()
    {
        return Db::table('xl_business_type')->where("status","=",1)
            ->select();

    }

    /**
     * 新增车辆信息
     * @return void
     */
    public static function add($data)
    {
        return Db::table('xl_parnter_line')->insertGetId($data);

    }

    /**
     * 编辑车辆信息
     * @return int
     */
    public static function edit($data,$id)
    {
        return Db::table('xl_parnter_line')->where('id','=',$id)->update($data);

    }


    /**
     * 删除车辆信息
     * @return int
     */
    public static function del($id)
    {
        return Db::table('xl_parnter_line')->where('id','=',$id)->update(['status'=>3]);

    }


}
